<template>
  <div class="authority">
    <el-card class="box-card">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/interface' }"
          >首页</el-breadcrumb-item
        >
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 搜索商品 -->
      <div class="users_title">
        <div class="ipt">
          <el-input
            placeholder="请输入内容"
            v-model="params.query"
            class="input-with-select"
            @keydown.enter="user_search1"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="user_search"
            ></el-button>
          </el-input>
        </div>
        <!-- 添加商品 -->
        <el-row>
          <el-button type="primary" @click="nav_goodsAdd">添加商品 </el-button>
        </el-row>
      </div>
      <!-- 商品列表 -->
      <div>
        <el-table
          :data="tableData"
          height="430px"
          border
          size="small"
          style="width: 100%"
        >
          <el-table-column
            type="index"
            align="center"
            width="50px"
            label="序号"
          >
          </el-table-column>
          <el-table-column
            prop="goods_name"
            align="center"
            label="商品名称"
            width="700px"
          >
          </el-table-column>
          <el-table-column
            prop="goods_price"
            align="center"
            label="商品价格(元)"
          >
            <template slot-scope="priceVal">
              {{ priceVal.row.goods_price | price }}
            </template>
          </el-table-column>
          <el-table-column prop="goods_weight" align="center" label="商品重量">
            <template slot-scope="weightVal">
              {{ weightVal.row.goods_weight | weight }}
            </template>
          </el-table-column>
          <el-table-column prop="add_time" align="center" label="创建时间">
            <template slot-scope="timeVal">
              {{ timeVal.row.add_time | timeFormat }}
            </template>
          </el-table-column>
          <el-table-column prop="address" align="center" label="操作">
            <template slot-scope="EachData">
              <!-- 编辑商品 -->
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="editArticle(EachData.row.goods_id)"
              ></el-button>
              <!-- 删除商品 -->
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-delete"
                @click="delArticle(EachData.row.goods_id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[50, 100, 300, 600]"
          :page-size="50"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 编辑商品对话框 -->
    <el-dialog title="编辑商品信息" :visible.sync="editCommodity" width="50%">
      <el-form
        :model="editCommodityForm"
        :rules="rules"
        ref="editCommodityForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editCommodityForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editCommodityForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editCommodityForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCommodity = false">取 消</el-button>
        <el-button type="primary" @click="editDity">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //请求对象
      params: {
        query: "",
        pagenum: 1,
        pagesize: 50,
      },
      currentPage4: 0,
      total: 0,
      //商品列表
      tableData: [],
      //编辑商品对话框
      editCommodity: false,
      editId: 0,
      editCommodityForm: {
        goods_name: "",
        goods_price: "",
        goods_number: 1,
        goods_weight: "",
      },
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "长度在 3 到 50 个字符",
            trigger: "blur",
          },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "请输入规范价格",
            trigger: "blur",
          },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "请输入规范重量范围",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //确定商品修改
    async editDity() {
      let res = await this.axios.put(
        `goods/${this.editId}`,
        this.editCommodityForm
      );
      console.log(res);
    },
    //编辑商品
    editArticle(id) {
      this.editCommodity = true;
      this.editId = id;
    },
    //删除商品
    delArticle(id) {
      this.MessageBox.confirm("此操作将删除该商品, 是否继续?", "温馨提示：", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios.delete(`goods/${id}`).then(
            (res) => {
              console.log(res);
              if (res.data.meta.status == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.commodityList();
              } else {
                this.$message({
                  type: "error",
                  message: "删除失败，权限不足！",
                });
              }
            },
            (err) => {
              console.log(err);
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //添加商品
    nav_goodsAdd() {
      this.$router.push("/interface/goods/add");
    },
    //查询商品
    user_search1() {
      // 键盘事件
      this.user_search();
    },
    user_search() {
      this.axios.get(`goods`, this.params).then(
        (res) => {
          this.$message({
            message: "查询商品成功!",
            type: "success",
            center: "true",
          });
          console.log(res);
          this.tableData = res.data.data;
          this.commodityList();
        },
        (err) => {
          console.error(err);
        }
      );
    },
    //获取商品列表
    commodityList() {
      this.axios
        .get("goods", {
          params: this.params,
        })
        .then(
          (res) => {
            console.log(res);
            this.tableData = res.data.data.goods;
            this.total = res.data.data.total;
          },
          (err) => {
            console.log(err);
          }
        );
    },
    //每页多少条数据
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.pagesize = val;
      this.commodityList();
    },
    //获取当页的数值
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.pagenum = val;
      this.commodityList();
    },
  },
  mounted() {
    this.commodityList();
  },
};
</script>

<style lang='less' scope>
.authority {
  width: 100%;
  .box-card {
    height: 82vh;
    //添加用户
    .users_title {
      width: 100%;
      height: 80px;
      display: flex;
      align-items: center;
      .el-form-item__label:before {
        content: "*";
        color: #f56c6c;
        margin-right: 4px;
      }
      .el-row {
        margin: 30px;
      }
      .ipt {
        .el-input__inner {
          width: 300px;
        }
      }
      .el-select .el-input {
        width: 100px;
      }
      .input-with-select .el-input-group__prepend {
        background-color: #fff;
      }
    }
    .btn {
      margin-top: 30px;
    }
    //角色列表
    .role {
      margin-top: 30px;
    }
    .border {
      margin: 10px 0px;
    }
    .block {
      height: 70px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>